package com.example.bottom_tab.tab;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.bottom_tab.R;

import java.util.ArrayList;
import java.util.List;

public class tabView extends LinearLayout {
    private int defaultTextColor , selectorTextColor ;
    private float defaultIconSize;
    private OnClickBottomListener mOnClickBottomListener;
    private List<ImageView> imageViewList = new ArrayList<>();
    private List<TextView> textViewList = new ArrayList<>();
    private int[] icons , selectorIcons;
    private String[] texts;

    public tabView(Context context) {
        this(context , null);
    }

    public tabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs , 0);
    }

    public tabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs , R.styleable.tabView);
        defaultTextColor = a.getColor(R.styleable.tabView_defaultTextColor , Color.parseColor("#333333"));
        selectorTextColor = a.getColor(R.styleable.tabView_selectorTextColor , Color.RED);
        defaultIconSize = a.getDimension(R.styleable.tabView_defaultIconSize , ScreenUtil.dp2px(context , 40));
        initView();
    }

    private void initView(){
        setOrientation(LinearLayout.HORIZONTAL);
    }

    //添加图标和文字
    public void addIconAndText(final int[] icons , final int[]selectorIcons , String[] texts){
        if(icons != null && texts != null && selectorIcons != null){
            this.icons = icons;
            this.selectorIcons = selectorIcons;
            this.texts = texts;
            updateAddViews();
        }
    }

    //天剑的icon和文字转成view
    private void updateAddViews(){
        if(icons.length == texts.length && icons.length == selectorIcons.length){
            int length = icons.length;
            removeAllViews();
            imageViewList.clear();
            textViewList.clear();
            for(int i = 0 ; i < length ; i ++){
                View childView = getItemView(icons[i] , texts[i]);
                addView(childView);
                if(i == 0){
                    setSelectorPosition(0);
                }
                final int finalI = i;
                childView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setSelectorPosition(finalI);
                        if(mOnClickBottomListener != null){
                            mOnClickBottomListener.clickView(v , finalI);
                        }
                    }
                });
            }
        }
    }
    //设置选中的第几个position
    public void setSelectorPosition(int position){
        updateTextColor(position);
        updateIcon(icons , selectorIcons , position);
    }
    private void updateTextColor(int position){
        if(textViewList.size() > position){
            for(int i = 0 ; i < textViewList.size() ; i ++){
                textViewList.get(i).setTextColor(defaultTextColor);
            }
            textViewList.get(position).setTextColor(selectorTextColor);
        }
    }
    private void updateIcon(int[] icons , int[] selectorIcons , int position){
        if(imageViewList.size() > position){
            for(int i  = 0 ; i < imageViewList.size() ; i ++){
                imageViewList.get(i).setImageResource(icons[i]);
            }
            imageViewList.get(position).setImageResource(selectorIcons[position]);
        }
    }
    //获取第几个item的view
    private View getItemView(int icon , String text){
        LinearLayout ll = new LinearLayout(getContext());
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0 , ViewGroup.LayoutParams.MATCH_PARENT);
        lp.weight = 1;
        lp.gravity = Gravity.CENTER;
        ll.setLayoutParams(lp);
        ImageView iv = new ImageView(getContext());
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.
                LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT);
        rlp.width = (int) defaultIconSize;
        rlp.height = (int) defaultIconSize;
        iv.setLayoutParams(rlp);
        iv.setImageResource(icon);
        ll.addView(iv);
        TextView tv = new TextView(getContext());
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setText(text);
        tv.setTextColor(defaultTextColor);
        tv.setTextSize(14);
        ll.addView(tv);
        imageViewList.add(iv);
        textViewList.add(tv);
        return ll;
    }
    public void setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        mOnClickBottomListener = onClickBottomListener;
    }
    public interface OnClickBottomListener{
        void clickView(View v , int position);
    }


}
